let now = new Date();
let year = now.getFullYear();
let month = now.getMonth();//真实的月份需要再加上1
let day = now.getDate();
let currentTime = new Date();

function dealChatTime(currentItemTimeStamp, frontItemTimeStamp) {
  let ifShowTime = timeDivide(currentItemTimeStamp, frontItemTimeStamp);
  return justSimpleDealTime(currentItemTimeStamp, ifShowTime);
}

function timeDivide(currentItemTimeStamp, frontItemTimeStamp) {
  // console.log('时间戳显示时间', currentItemTimeStamp, frontItemTimeStamp);
  return Math.abs(currentItemTimeStamp - frontItemTimeStamp) / 1000 > 300
}

function justSimpleDealTime(currentItemTimeStamp, ifShowTime) {
  currentTime.setTime(currentItemTimeStamp);
  let hoursAndMinutes = currentTime.getHours() + ':' + (currentTime.getMinutes() >= 10 ? currentTime.getMinutes() : ('0' + currentTime.getMinutes()));
  let currentTimeDay = currentTime.getDate();
  if (currentTime.getFullYear() === year && currentTime.getMonth() === month) {
    if (currentTimeDay === day) {//当天显示时分
      return {//5分钟内发送多条消息时不重复显示时间标签,大于5分钟显示时间标签
        ifShowTime: ifShowTime,
        timeStr: hoursAndMinutes
      };
    } else if (currentTimeDay === day - 1) {//昨天：昨天+时分（24小时制）
      return { ifShowTime: ifShowTime, timeStr: '昨天 ' + hoursAndMinutes }
    }
  }
  return {
    ifShowTime: ifShowTime,
    timeStr: currentTime.getFullYear() + '年' + (currentTime.getMonth() + 1) + '月' + currentTimeDay + '日 ' + hoursAndMinutes
  };
}

/** 
 * 获取前三十天的所有日期   
 */
function lastThirtyAllDay(lastNum = 0) {
  var list = [];
  for (var i = 0; i < 30; i++) {
    var nowDate = new Date();
    nowDate.setDate(nowDate.getDate() - i);//获取AddDayCount天后的日期 
    var y = nowDate.getFullYear();
    var m = nowDate.getMonth() + 1;//获取当前月份的日期 
    var d = nowDate.getDate();
    if (m < 10) {
      m = "0" + m;
    }
    if (d < 10) {
      d = "0" + d;
    }
    var text = m + "月" + d + "日";
    if (i == 0) {
      text = "今日";
    }
    var value = y + "-" + m + "-" + d;
    var data = { "text": text, "value": value };
    list.push(data);
  }
  return list;
}

/**
 * 获取24小时
 */
function getAllHours() {
  var list = [];
  for (var i = 0; i < 24; i++) {
    if (i < 10) {
      i = "0" + i;
    }
    var text = i + "时";
    var value = i;
    var data = { "text": text, "value": value };
    list.push(data);
  }
  return list;
}

/**
 * 获取60分
 */
function getAllMinutes() {
  var list = [];
  for (var i = 0; i < 60; i++) {
    if (i < 10) {
      i = "0" + i;
    }
    var text = i + "分";
    var value = i;
    var data = { "text": text, "value": value };
    list.push(data);
  }
  return list;
}

function calculateDifferDay(date1, date2) {
  var dateTime1 = new Date(date1.replace(/-/g, "/")).getTime();
  var dateTime2 = new Date(date2.replace(/-/g, "/")).getTime();
  var day = (dateTime1 - dateTime2) / (1000 * 60 * 60 * 24);
  return day;
}

function getDateTime(date){
  return new Date(date.replace(/-/g, "/")).getTime();
}

module.exports = {
  dealChatTime,
  lastThirtyAllDay,
  getAllHours,
  getAllMinutes,
  calculateDifferDay,
  getDateTime
};